/* 
 * Copyright 2011 1chu.com, Inc. All rights reserved.
 * 
 * Created on Mar 4, 2011 By aroot
 */
package com.fadadianqi.commons;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @autor aroot
 */
public class Constants {

    private static final Log log = LogFactory.getLog(Constants.class);

    public static String CLASS_PATH = null;
    public static final String DEFAULT_CHARSET = "UTF-8";
    public static String ROOT_PATH = null;
    public static String PARENT_PATH = null;
    public static String WEB_INF_PATH = null;
    public static String UTF_8 = "UTF-8";
    public static String GBK = "GBK";
    public static String ISO_8895_1 = "ISO-8859-1";

    public static List<String> ALPHABET = null;

    public static ClassLoader getDefaultClassLoader() {
        ClassLoader cl = null;
        try {
            cl = Thread.currentThread().getContextClassLoader();
        } catch (Throwable ex) {
            log.error(
                    "Cannot access thread context ClassLoader - falling back to system class loader",
                    ex);
        }
        return cl;
    }

    static {
        if (ALPHABET == null) {
            ALPHABET = new ArrayList<String>();
            ALPHABET.add("A");
            ALPHABET.add("B");
            ALPHABET.add("C");
            ALPHABET.add("D");
            ALPHABET.add("E");
            ALPHABET.add("F");
            ALPHABET.add("G");
            ALPHABET.add("H");
            ALPHABET.add("I");
            ALPHABET.add("J");
            ALPHABET.add("K");
            ALPHABET.add("L");
            ALPHABET.add("M");
            ALPHABET.add("N");
            ALPHABET.add("O");
            ALPHABET.add("P");
            ALPHABET.add("Q");
            ALPHABET.add("R");
            ALPHABET.add("S");
            ALPHABET.add("T");
            ALPHABET.add("U");
            ALPHABET.add("V");
            ALPHABET.add("W");
            ALPHABET.add("X");
            ALPHABET.add("Y");
            ALPHABET.add("Z");
        }
        if (ROOT_PATH == null) {
            if (ROOT_PATH == null) {
                String tmp = Constants.class.getProtectionDomain()
                        .getCodeSource().getLocation().getPath();
                System.out
                        .println("Constants...getCodeSource().getLocation(): "
                                + Constants.class.getProtectionDomain()
                                .getCodeSource().getLocation());

                System.out
                        .println("Constants...getCodeSource().getLocation().getPath(): "
                                + Constants.class.getProtectionDomain()
                                .getCodeSource().getLocation()
                                .getPath());

                String separator = File.separator;

                if (System.getProperty("os.name").toLowerCase()
                        .indexOf("windows") != -1) {
                    separator = "/";
                    if (tmp.startsWith("file:/")) {
                        tmp = tmp.replace("file:/", "");
                    } else if (tmp.startsWith("/") && tmp.length() > 1) {
                        tmp = tmp.substring(1, tmp.length());
                    }
                }
                int i = tmp.indexOf("classes");
                int n = tmp.indexOf(".jar");
                System.out.println("os.name: " + System.getProperty("os.name"));
                System.out.println("tmp(1): " + tmp);
                System.out.println("File.separator: " + separator);
                if (i != -1) {
                    ROOT_PATH = tmp.substring(0, i);
                } else if (n != -1) {
                    ROOT_PATH = tmp
                            .substring(0, tmp.lastIndexOf(separator) + 1);
                } else {
                    ROOT_PATH = tmp;
                }
                System.out.println("ROOT_PATH(1): " + ROOT_PATH);
                if (ROOT_PATH.endsWith("lib" + separator)) {
                    ROOT_PATH = ROOT_PATH.substring(0, ROOT_PATH.length() - 4);
                } else if (ROOT_PATH.endsWith("lib")) {
                    ROOT_PATH = ROOT_PATH.substring(0, ROOT_PATH.length() - 3);
                }
                System.out.println("ROOT_PATH(2): " + ROOT_PATH);
                PARENT_PATH = new File(ROOT_PATH).getParent() + separator;
                if (CLASS_PATH == null) {
                    CLASS_PATH = System.getProperty("java.class.path");
                }
                WEB_INF_PATH = ROOT_PATH + "classes" + separator;
                System.out.println("Constants#ROOT_PATH: " + ROOT_PATH
                        + ", PARENT_PATH: " + PARENT_PATH + ", CLASS_PATH: "
                        + CLASS_PATH);
            }
        }
    }

    public static final String getBaseDir() {
        return System.getProperty("base.dir", "..");
    }
}
